//
//  AnimationViewController.m
//  AnimationDemo
//
//  Created by Jiaozl 2018 on 2018/9/17.
//  Copyright © 2018年 Jiaozl. All rights reserved.
//

#import "AnimationViewController.h"
#import "ZLWaveView.h"
#import "ZLSpringAnimationView.h"
#import "ThrowLineTool.h"
#import "BasicAnimationView.h"
#import "KeyFrameAnimationView.h"
#import "ZLTransitionView.h"


@interface AnimationViewController ()

@property (nonatomic, strong) KeyFrameAnimationView *keyAniView;
@property (nonatomic, strong) ZLTransitionView *transitionAniView;

@end

@implementation AnimationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setUpUI:self.animationType];
}

- (void)setUpUI:(ZLAnimationType)type{
    switch (type) {
        case ZLAnimationTypeWave:
        {
            CGRect rect = {{0, 64}, {self.view.bounds.size.width, self.view.bounds.size.height}};
            ZLWaveView  *waveView = [[ZLWaveView alloc] initWithFrame:rect];
            waveView.backgroundColor = UIColor.whiteColor;
            [self.view addSubview:waveView];
            
        }
            break;
        case ZLAnimationTypeSpringAnimation:
        {
            [self springAnimation];
        }
            break;
        case ZLAnimationTypeThrowLine:
        {
            UIView *ballView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
            ballView.backgroundColor = UIColor.cyanColor;
            [self.view addSubview:ballView];
            
            [self beginThrowing:ballView];
        }
            break;
        case ZLAnimationTypeBasic:
        {
            [self basicAnimation];
        }
            break;
            
        case ZLAnimationTypeKeyframe:
        {
            [self keyFrameAnimation];
        }
            break;
            
        case ZLAnimationTypeTransition:
        {
            [self transitionView];
        }
            break;
        case ZLAnimationTypeAffine:
        {
            [self pushToVC:@"AffineTransformController" withTitle:@"仿射变换动画"];
        }
            break;
            
        default:
            break;
    }
}

- (void)pause:(UIButton *)sender {
    
    if (self.keyAniView) {
        if (sender.selected) {
            [self.keyAniView resumeRectRunLayer];
        } else {
            [self.keyAniView pauseRectRunLayer];
        }
        sender.selected = !sender.selected;
    }
    
}

#pragma mark - 转场动画

- (void)transitionView {
    self.transitionAniView = [[ZLTransitionView alloc] initWithFrame:CGRectMake(100, 100, 200, 300)];
    
    self.transitionAniView.backgroundColor = UIColor.redColor;
    self.transitionAniView.numberOfLines = 0;
    self.transitionAniView.textColor = UIColor.whiteColor;
    self.transitionAniView.font = [UIFont systemFontOfSize:20];
    self.transitionAniView.textAlignment = NSTextAlignmentCenter;
    
    [self.view addSubview:self.transitionAniView];
}
#pragma mark - 关键帧动画

- (void)keyFrameAnimation {
    self.keyAniView = [[KeyFrameAnimationView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height)];
    self.keyAniView.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:self.keyAniView];
    
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(80, self.view.bounds.size.height-50, 80, 30)];
    [button setTitle:@"暂停" forState:UIControlStateNormal];
    [button setTitle:@"恢复" forState:UIControlStateSelected];
    [button setTitleColor:UIColor.redColor forState:UIControlStateNormal & UIControlStateSelected];
    [button addTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
#pragma mark - 基础动画

- (void)basicAnimation {
    BasicAnimationView *aniView = [[BasicAnimationView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height)];
    aniView.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:aniView];
}
#pragma mark - 弹性动画

- (void) springAnimation{
    ZLSpringAnimationView *springAniView = [[ZLSpringAnimationView alloc] initWithFrame:CGRectMake(80, 100, 100, 80)];
    springAniView.backgroundColor = UIColor.redColor;
    [self.view addSubview:springAniView];
    
    UIView *lineview = [[UIView alloc] initWithFrame:CGRectMake(80, 100, 5, 80)];
    lineview.backgroundColor = [UIColor greenColor];
    [self.view addSubview:lineview];
    
    UIView *lineview2 = [[UIView alloc] initWithFrame:CGRectMake(280, 100, 5, 80)];
    lineview2.backgroundColor = [UIColor greenColor];
    [self.view addSubview:lineview2];
}

#pragma mark - 抛物线动画

- (void)beginThrowing:(UIView *)view
{
    ThrowLineTool *tool = [ThrowLineTool sharedTool];
    tool.delegate = self;
    //    UIImageView *bagImgView = (UIImageView *)[self.view viewWithTag:1000];
    CGFloat startX = 0;//arc4random() % (NSInteger)CGRectGetWidth(self.frame);
    CGFloat startY = 150;//CGRectGetHeight(self.frame);
    CGFloat endX = CGRectGetMidX(self.view.frame) + 10 - (arc4random() % 50);
    CGFloat endY = CGRectGetMidY(self.view.frame);
    CGFloat height = 50 + arc4random() % 40;
    [tool throwObject:view
                 from:CGPointMake(startX, startY)
                   to:CGPointMake(endX, endY)
               height:height duration:1.6];
}

@end
